package cn.tao.user.service.impl;

import cn.tao.base.exception.BusinessException;
import cn.tao.base.query.BaseQuery;
import cn.tao.base.service.impl.BaseServiceImpl;
import cn.tao.user.domain.LoginInfo;
import cn.tao.user.domain.User;
import cn.tao.user.domain.dto.UserDto;
import cn.tao.user.mapper.LoginInfoMapper;
import cn.tao.user.mapper.UserMapper;
import cn.tao.user.service.IUserService;
import cn.tao.utils.MD5Utils;
import cn.tao.utils.QueryResult;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {


    @Autowired
    private UserMapper userMapper;
    //注入redis核心对象
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public User loadByPhone(String phone) {
        User user = userMapper.loadByPhone(phone);
        return user;
    }


    /*
        1.把userdto转为user
        2.把密码加密处理
        3.把user对象转为logInfo
        4.存储对象，先存储loginfo 返回主键id 然后给user中的loginfo_id字段添加
        进行逻辑操作之前，在后台还要进行一次简单的逻辑判断
     */
    @Override
    @Transactional
    public void register(UserDto userDto) throws BusinessException {
        verifyData(userDto);
        User user = userDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);
        //保存数据
        loginInfoMapper.save(loginInfo);
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);

    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setDisable(user.getState());
        //1 ：前端用户
        loginInfo.setType(1);
        return loginInfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername("p_"+userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //通过uuid拿到一组随机数
        String salt = UUID.randomUUID().toString();
        //使用MD5加密 密码+盐 一起加密
        String md5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        // 0待激活 1启用 -1 封号
        user.setState(1);
        user.setCreatetime(new Date());
        return user;
    }

    /**
     * 生成一个私有化方法，在内部进行简单的判断
     * @param userDto
     */
    private void verifyData(UserDto userDto) throws BusinessException {
        //1.手机号不能为空  isNullOrEmpty判断字符串是否为空 返回true
        if (StringUtils.isNullOrEmpty(userDto.getPhone())){
            throw new BusinessException("手机号不能为空");
        }
        //2.验证码不能为空
        if (StringUtils.isNullOrEmpty(userDto.getCode())){
            throw new BusinessException("验证码不能为空");
        }
        //3.密码不能为空
        if (StringUtils.isNullOrEmpty(userDto.getPassword())){
            throw new BusinessException("密码不能为空");
        }
        //4.确认密码不能为空
        if (StringUtils.isNullOrEmpty(userDto.getPasswordRepeat())){
            throw new BusinessException("确认密码不能为空");
        }
        //5.验证码是否正确
        String codeInRedis = (String) redisTemplate.opsForValue().get(userDto.getPhone() + ":" + userDto.getCodeType());
        //把验证码取出来
        if (!codeInRedis.split(":")[0].equals(userDto.getCode()) ||codeInRedis==null){
            throw new BusinessException("验证码无效");
        }
    }


}
